﻿var User = require('/node/chat/models/user').User;
var HttpError = require('/node/chat/error').HttpError;
var AuthError = require('/node/chat/models/user').AuthError;
var async = require ('async');

exports.get = function(req, res) {
  res.render('login');
}

exports.post = function(req, res, next) {
  var username = req.body.username;
  var password = req.body.password;
  
  User.authorize(username, password, function(err, user) {
    if (err) {
	  if (err instanceof AuthError) {
	    return next(new HttpError(403, err.message));
	  } else {
	  return next(err);
	  }
	}
	
	req.session.user = user._id;
	res.send({});
	
  });
  
};